﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Dijkstra_Belman_Ford
{
    public partial class Form1 : Form
    {
        private bool czyWygenerowano;
        private string file;
        private Graf g;
        private int size;
        public int counter=0;
        public double[] time1 = new double[1];
        public double[] time2 = new double[1];
        public int[] egdges = new int[1];

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            file = null;
            openFileDialog1.Title = "Explorer";
            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.Filter = "Data File| *.Dat";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName.ToString();
                FileStream fileStr = (FileStream)openFileDialog1.OpenFile();
                StreamReader reader = new StreamReader(fileStr);
                file += reader.ReadToEnd();
                czyWygenerowano = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!czyWygenerowano)
            {
                string[] fFile = file.Split(new char[] { '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int z = 1, licznik = 0, k = 0;
                size = int.Parse(fFile[0]);
                g = new Graf(size);
                for (int i = 0; i < fFile.Length - 1; i++)
                {
                    if (i == 0)
                    {
                        for (int j = 0; j < int.Parse(fFile[1]); j++)
                        {
                            g.Add(k, int.Parse(fFile[z + 1]) - 1, int.Parse(fFile[z + 2]));
                            z += 2;
                        }
                        k++;
                    }
                    else if (fFile[i] == "\r")
                    {
                        licznik = i + 1;

                        z = licznik;

                        for (int j = 0; j < int.Parse(fFile[licznik]); j++)
                        {
                            g.Add(k, int.Parse(fFile[z + 1]) - 1, int.Parse(fFile[z + 2]));
                            z += 2;
                        }
                        k++;
                    }
                }
            }
            else g = FormGenerate.Graph;
            richTextBox1.Clear();
            g.Bellman_Ford(size);
            g.Dijkstry(size);

            richTextBox1.Text = g.ShowGraph(size);
            richTextBox3.Text = g.ShowBellmanFord(size);
            richTextBox2.Text = g.ShowDijkstry(size);
            textBox2.Text = g.ShowTimeDijkstry();
            textBox3.Text = g.ShowTimeBellmanFord();

            time1[counter] = g.ShowTimeD();
            time2[counter] = g.ShowTimeBF();
            egdges[counter] = g.CountEdges(size);

            counter++;

            Array.Resize(ref time1,counter+1);
            Array.Resize(ref time2, counter+1);
            Array.Resize(ref egdges, counter+1);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Owner = this; 
            form2.Show();
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FormGenerate fG = new FormGenerate();
            fG.Show();
            czyWygenerowano = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SimulateAndWrite SAW = new SimulateAndWrite();
            SAW.Show();
        }
    }
}
